﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using DMS.Business.Utility;
using System.Web.UI.WebControls;

namespace DMS.BaseClass
{
    public class ListControlBase : System.Web.UI.UserControl
    {
        private ListModeTypes _Mode = ListModeTypes.Management;
        public ListModeTypes Mode
        {
            get { return _Mode; }
            set { _Mode = value; }
        }

        protected virtual List<Control> ControlSet
        {
            get
            {
                return new List<Control>();
            }
        }
        protected virtual List<string> ControlSetInGrid
        {
            get
            {
                return new List<string>();
            }
        }
        protected virtual Dictionary<int, string> ColumnStateSettings
        {
            get
            {
                return new Dictionary<int, string>();
            }
        }
        protected virtual WebControl MainListControl
        {
            get
            {
                return null;
            }
        }
        protected override void OnPreRender(EventArgs e)
        {
            this.SetControlState();
            this.SetGridState();
            base.OnPreRender(e);
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (this.MainListControl == null)
            {
                return;
            }

            DataGrid dg = this.MainListControl as DataGrid;
            GridView gv = this.MainListControl as GridView;
            if (dg != null)
            {
                dg.ItemDataBound += new DataGridItemEventHandler(MainListControl_ItemDataBound);
            }
            else if (gv != null)
            {
                gv.RowDataBound += new GridViewRowEventHandler(MainListControl_RowDataBound);
            }
        }

        void MainListControl_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            DataGridItem row = e.Item;
            if (row.ItemType != ListItemType.Item || row.ItemType != ListItemType.AlternatingItem)
            {
                return;
            }

            List<string> ctrlIds = this.ControlSetInGrid;
            foreach (string ctrlId in ctrlIds)
            {
                Control ctrl = row.FindControl(ctrlId) as Control;
                // Visible
                ClientHelper.Instance.SetDisplayStatue(ctrl, this._Mode.ToString());
            }
        }

        private void MainListControl_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            GridViewRow row = e.Row;
            if (row.RowType != DataControlRowType.DataRow)
            {
                return;
            }

            List<string> ctrlIds = this.ControlSetInGrid;
            foreach (string ctrlId in ctrlIds)
            {
                Control ctrl = row.FindControl(ctrlId) as Control;
                // Visible
                ClientHelper.Instance.SetDisplayStatue(ctrl, this._Mode.ToString());
            }
        }
        private void SetControlState()
        {
            List<Control> ctrls = this.ControlSet;
            foreach (Control ctrl in ctrls)
            {
                // ReadOnly
                ClientHelper.Instance.SetReadOnlyStatue(ctrl, this._Mode.ToString());
                // Visible
                ClientHelper.Instance.SetDisplayStatue(ctrl, this._Mode.ToString());
            }
        }

        private void SetGridState()
        {
            string currentListMode = this._Mode.ToString().ToLower();
            if (this.ColumnStateSettings == null || this.ColumnStateSettings.Count == 0)
            {
                return;
            }

            DataGrid dg = this.MainListControl as DataGrid;
            GridView gv = this.MainListControl as GridView;
            if (dg != null)
            {
                foreach (int colIndex in this.ColumnStateSettings.Keys)
                {
                    if (colIndex >= dg.Columns.Count)
                    {
                        continue;
                    }
                    DataGridColumn col = dg.Columns[colIndex];
                    col.Visible = this.ColumnStateSettings[colIndex].ToLower().Contains(currentListMode);
                }
            }
            else if (gv != null)
            {
                foreach (int colIndex in this.ColumnStateSettings.Keys)
                {
                    if (colIndex >= gv.Columns.Count)
                    {
                        continue;
                    }
                    DataControlField col = gv.Columns[colIndex];
                    col.Visible = this.ColumnStateSettings[colIndex].ToLower().Contains(currentListMode);
                }
            }
        }
    }
}
